<template>
  <div>
    <el-dialog
      v-model="open"
      :close-on-click-modal="false"
      :title="title"
    >
      <el-form
        ref="editForm"
        label-width="130px"
        :rules="rules"
        :model="form"
        inline
      >
        <el-form-item label="亚马逊编码" prop="code">
          <el-input
            v-model="form.code"
           
            clearable
            placeholder="请输入亚马逊编码"
          ></el-input>
        </el-form-item>
        <el-form-item label="地址" prop="address">
          <el-input
            v-model="form.address"
         
            clearable
            placeholder="请输入地址"
          ></el-input>
        </el-form-item>
        <el-form-item label="城市" prop="city">
          <el-input
            v-model="form.city"
          
            clearable
            placeholder="请输入城市"
            @blur="form.city = form.city.toString().trim()"
          ></el-input>
        </el-form-item>
        <el-form-item label="省州" prop="state">
          <el-input
            v-model.trim="form.state"
           
            clearable
            placeholder="请输入省州"
          ></el-input>
        </el-form-item>
        <el-form-item label="邮编" prop="postCode">
          <el-input
            v-model.trim="form.postCode"
            
            clearable
            placeholder="请输入邮编"
          ></el-input>
        </el-form-item>
      </el-form>
      <template #footer>
        <div>
          <el-button type="primary" @click="submitForm">确定</el-button>
          <el-button @click="cancel">取消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>
<script lang="ts" setup>
import { ref, defineExpose, getCurrentInstance } from 'vue'
import { ElMessage, ElMessageBox, ElButton } from 'element-plus'
import { resetForm } from '@/utils'
import { getData, delData, editData, addData, getDescription } from '@/api/BusinessConfig/amazonAddressManagement'

const emit = defineEmits(['changeList'])
// 表单状态
let open = ref(false),
  title = ref(null),
  // 表单参数
  form = ref({
    id: undefined,
    code: undefined,
    address: undefined,
    city: undefined,
    state: undefined,
    postCode: undefined,
  }),
  // 规则
  rules = {
    code: [
      {
        required: true,
        message: '亚马逊编码不能为空',
        trigger: ['blur'],
      },
    ],
    address: [
      {
        required: true,
        message: '地址不能为空',
        trigger: ['blur'],
      },
    ],
    city: [
      {
        required: true,
        message: '城市不能为空',
        trigger: ['blur'],
      },
    ],
    state: [
      {
        required: true,
        message: '省州不能为空',
        trigger: ['blur'],
      },
    ],
    postCode: [
      {
        required: true,
        message: '邮编不能为空',
        trigger: ['blur'],
      },
    ],
  },
  editForm = ref(null),
  {
    appContext: {
      config: {
        globalProperties: { $resetForm, $validateForm },
      },
    },
  } = getCurrentInstance()
// 重置表单
const reset = () => {
  form.value = {
    id: undefined,
    code: undefined,
    address: undefined,
    city: undefined,
    state: undefined,
    postCode: undefined,
  }
  $resetForm(editForm.value)
}
// 新增-修改表单
const submitForm = () => {
  $validateForm(editForm.value, () => {
    let msg = ''
    let response = null
    if (form.value.id != undefined) {
      msg = '修改成功'
      response = editData(form.value)
    } else {
      msg = '新增成功'
      response = addData(form.value)
    }
    response
    .then((res) => {
      if (res.code === 200) {
        ElMessage.success(msg)
        open.value = false
        emit('changeList')
        reset()
      } else {
        ElMessage.error(res.msg)
      }
    })
    .catch((error) => {
      ElMessage.error(error)
      reset()
    })
  })
}
// 查询-编辑
const getList = (id) => {
  getDescription(id)
  .then((res) => {
    if (res.code === 200) {
      form.value = res.data
    } else {
      ElMessage.error(res.msg)
    }
  })
  .catch((err) => {
    console.log(err)
    ElMessage.error(err)
  })
}
// 取消新增-修改
const cancel = () => {
  open.value = false
  reset()
}

defineExpose({
  open,
  title,
  form,
  getList,
})
</script>
